#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import hashlib
import os
import string

class HashBuilderError(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)

class HashBuilder:
    def __init__(self, name):
        self.__name = name

    def get_hash(self, data):
        hash = hashlib.new(self.__name)
        hash.update(data)

        return hash.hexdigest()
        
    def get_file_hash(self, filename):
        """Calcutes hash of filename"""
        hash = hashlib.new(self.__name)
        fd = None
        try:
            fd = open(filename, "rb")
            size = os.path.getsize(filename)            
            hash.update(fd.read(size))
        except Exception, error:
            raise HashBuilderError(error.message)
        finally:
            if fd: fd.close()
        
        return hash.hexdigest()
